<template>
	<component ref="wyObject" :is="Form" :formData="wyform" :rules="rules" :save="save"></component>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { post, get } from '@/axios'
import { ElMessage } from 'element-plus'
import Form from './components/form'
const props = defineProps(['getTable', 'id', 'urls'])
const emits = defineEmits(['update:show'])
const wyObject = ref()
const wyform = reactive({
	serialNumber: '',
	nursingName: '',
	servicePrice: '',
	message: '',
	status: null,
	executionCycle: '',
	executionTimes: null,
})
function check(field, value, callback) {
	get(props.urls.check, { field, value: value.trim(), id: props.id }, content => {
		if (content) {
			callback()
		} else {
			callback(new Error())
		}
	})
}
const rules = {
	serialNumber: [
		{ required: true, message: '请输入护理项目编号', trigger: 'blur' },
		{ max: 20, message: '护理项目编号最多20个字符', trigger: 'blur' },
		{ validator: (rule, value, callback) => { check('serial_number', value, callback) }, message: '护理编号已经存在', trigger: 'blur' }
	],
	nursingName: [
		{ required: true, message: '请输入护理项目名称', trigger: 'blur' },
		{ max: 55, message: '护理项目名称最多55个字符', trigger: 'blur' }
	],
	servicePrice: [
		{ required: true, message: '请输入护理价格', trigger: 'blur' },
		{ max: 55, message: '护理价格最多55个字符', trigger: 'blur' }
	],
	status: [
		{ required: true, message: '请选择状态', trigger: 'blur' },
	],
	executionCycle: [
		{ required: true, message: '请输入护理周期', trigger: 'blur' },
		{ max: 20, message: '护理周期最多20个字符', trigger: 'blur' }
	],
	executionTimes: [
		{ required: true, message: '请输入执行次数', trigger: 'blur' }
	]
}
if (props.id) {
	get(props.urls.getById, { id: props.id }, content => {
		for (const key in wyform) {
			wyform[key] = content[key]
		}
		wyform.id = props.id
	})
}
function save() {
	post(props.id ? props.urls.update : props.urls.add, wyform, content => {
		emits('update:show', false)
		props.getTable()
	}, wyObject.value.formObj)
}
</script>

<style scoped lang="scss">
</style>